﻿using System;
using System.Data;
using System.IO;
using Soveryn.Commerce.Shared;
using System.Web;

/// <summary>
/// This class is for caching data from the Soveryn Commerce Core
/// </summary>
/// 
namespace Soveryn.Commerce.API
{

    public class CacheUtils
    {

        /// <summary>
        /// This deletes the product image cache, then recreates it with the dimensions defined by website settings
        /// </summary>
        public static void UpdateProductImagesCache()
        {

            WebsiteProperties Website = WebsiteData.WebsiteDetail();

            String[] cacheFiles = Directory.GetFiles(HttpContext.Current.Server.MapPath("/ProductImagesCache/"));
            for (int i = 0; i < cacheFiles.Length; i++)
                File.Delete(cacheFiles[i]);

            String[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Files/ProductImageUploads/"));
            for (int i = 0; i < files.Length; i++)
                ImageUtils.CreateImageCache("/Files/ProductImageUploads/", "/ProductImagesCache/", files[i].Substring(files[i].LastIndexOf("\\") + 1), Website.LargeImageSize.Value, Website.MediumImageSize.Value, Website.SmallImageSize.Value);

        }

        /// <summary>
        /// This deletes the site image cache, then recreates it with the dimensions defined by website settings
        /// </summary>
        public static void UpdateSiteImagesCache()
        {

            WebsiteProperties Website = WebsiteData.WebsiteDetail();

            String[] cacheFiles = Directory.GetFiles(HttpContext.Current.Server.MapPath("/SiteImagesCache/"));
            for (int i = 0; i < cacheFiles.Length; i++)
                File.Delete(cacheFiles[i]);

            String[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Files/SiteImageUploads/"));
            for (int i = 0; i < files.Length; i++)
                ImageUtils.CreateImageCache("/Files/SiteImageUploads/", "/SiteImagesCache/", files[i].Substring(files[i].LastIndexOf("\\") + 1), Website.LargeImageSize.Value, Website.MediumImageSize.Value, Website.SmallImageSize.Value);


        }

        /// <summary>
        /// This transforms a cached dataset in XML form back into a usable dataset
        /// </summary>
        public static DataSet GetCachedDataSet(String folder, String key)
        {

            String cacheFile = HttpContext.Current.Server.MapPath("~/App_Data/Cache/" + folder + "/" + key + ".xml");

            DataSet dataSet = null;

            if (File.Exists(cacheFile))
            {
                try
                {

                    dataSet = new DataSet();
                    dataSet.ReadXml(cacheFile);
                    dataSet.AcceptChanges();

                }
                catch (Exception ex)
                {
                    MiscUtils.ErrorLog(ex, "");
                    dataSet = null;
                }
            }

            return dataSet;

        }

        /// <summary>
        /// This saves the XML form of a dataset as a cache item
        /// </summary>
        public static void SetCachedDataSet(String folder, String key, DataSet dataSet)
        {
            try
            {
                String cacheFile = HttpContext.Current.Server.MapPath("~/App_Data/Cache/" + folder + "/" + key + ".xml");
                File.WriteAllText(cacheFile, dataSet.GetXml());
            }
            catch (Exception ex)
            {
                MiscUtils.ErrorLog(ex, "");
            }

        }

        /// <summary>
        /// This deletes a specific item in a cache folder
        /// </summary>
        public static void KillCachedItem(String folder, String key)
        {

            String cacheFile = HttpContext.Current.Server.MapPath("~/App_Data/Cache/" + folder + "/" + key + ".xml");

            try
            {
                File.Delete(cacheFile);
            }
            catch (Exception ex)
            {
                MiscUtils.ErrorLog(ex, "");
            }
        }

        /// <summary>
        /// This deletes all cache folders
        /// </summary>
        public static void ClearCache()
        {
            try
            {
                CacheUtils.UpdateProductImagesCache();
                CacheUtils.UpdateSiteImagesCache();
                CacheUtils.KillCache("Item");
                CacheUtils.KillCache("ItemList");
                CacheUtils.KillCache("Page");
                CacheUtils.KillCache("Website");
                CacheUtils.KillCache("Blog");
            }
            catch (Exception ex)
            {
                MiscUtils.ErrorLog(ex, "ClearCache");
            }

        }

        /// <summary>
        /// This deletes a cache folder
        /// </summary>
        public static void KillCache(String folder)
        {
            try
            {
                String[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/App_Data/Cache/" + folder));
                for (int i = 0; i < files.Length; i++)
                    File.Delete(files[i]);
            }
            catch (Exception ex)
            {
                MiscUtils.ErrorLog(ex, "KillCache - " + folder);
            }
        }

    }
}